<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="mealName">
        <el-input
          v-model="queryParams.mealName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="性别" clearable size="small">
          <el-option
            v-for="item in sexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <!--表格开始-->
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">

      <!--   附  -->
      <el-table-column type="expand" prop="setmealCheckGroupList">
        <template slot-scope="scope">
          <el-table
            :data="scope.row.setmealCheckGroupList"
            style="width: 100%">
            <el-table-column label="编号" align="center" prop="checkGroup.code"/>
            <el-table-column label="检查组名称" align="center" prop="checkGroup.name"/>
            <el-table-column label="助记码" align="center" prop="checkGroup.helpCode"/>
            <el-table-column label="所属性别" align="center" prop="checkGroup.sex">
              <template slot-scope="scope">
                <p v-if="scope.row.checkGroup.sex == '0'">全部</p>
                <p v-if="scope.row.checkGroup.sex == '1'">男</p>
                <p v-if="scope.row.checkGroup.sex == '2'">女</p>
              </template>
            </el-table-column>
            <el-table-column label="说明" align="center" prop="checkGroup.attention" width="180"/>
            <el-table-column label="备注说明" align="center" prop="checkGroup.remark" width="180"/>
          </el-table>
        </template>
      </el-table-column>
      <!--      主-->
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名字" align="center" prop="mealName"/>
      <el-table-column label="编码" align="center" prop="code"/>
      <el-table-column label="助记码" align="center" prop="helpCode"/>
      <el-table-column label="所属性别" align="center" prop="sex">
        <template slot-scope="scope">
          <p v-if="scope.row.sex == '0'">全部</p>
          <p v-if="scope.row.sex == '1'">男</p>
          <p v-if="scope.row.sex == '2'">女</p>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="价格" align="center" prop="price"/>
      <el-table-column label="评述" align="center" prop="remark"/>
      <el-table-column label="注意" align="center" prop="attention"/>
      <!--图片的查找-->
      <el-table-column label="图片" align="center" prop="img">
        <template slot-scope="scope">
          <el-image :src="baseUrl + scope.row.img"/>
        </template>
      </el-table-column>


      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="mealName">
          <el-input v-model="form.mealName" placeholder="请输入名字" style="width: 150px" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" style="width: 100px"/>
        </el-form-item>
        <el-form-item label="助记码" prop="helpCode">
          <el-input v-model="form.helpCode" placeholder="请输入助手" style="width: 100px"/>
        </el-form-item>
        <el-form-item label="所属性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio label="0">全部</el-radio>
            <el-radio label="1">男</el-radio>
            <el-radio label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" style="width: 100px"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" style="width: 100px"/>
        </el-form-item>
        <el-form-item label="评述" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入评述" style="width: 500px"/>
        </el-form-item>
        <el-form-item label="注意" prop="attention">
          <el-input v-model="form.attention" placeholder="请输入注意" style="width: 400px"/>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <image-upload v-model="form.img" limit="1"/>
        </el-form-item>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAddGroups(form.id)"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handelDeleteGroups"
            >删除</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="form.setmealCheckGroupList" @selection-change="handleSelectionChangeGroups">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="checkGroup.code"/>
          <el-table-column label="检查组名称" align="center" prop="checkGroup.name"/>
          <el-table-column label="助记码" align="center" prop="checkGroup.helpCode"/>
          <el-table-column label="所属性别" align="center" prop="checkGroup.sex">
            <template slot-scope="scope">
              <p v-if="scope.row.checkGroup.sex == '0'">全部</p>
              <p v-if="scope.row.checkGroup.sex == '1'">男</p>
              <p v-if="scope.row.checkGroup.sex == '2'">女</p>
            </template>
          </el-table-column>
          <el-table-column label="说明" align="center" prop="checkGroup.attention" width="180"/>
          <el-table-column label="备注说明" align="center" prop="checkGroup.remark" width="180"/>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="openAddCheckItem" width="1000px" append-to-body>
      <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionAddCheckGroupChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="code"/>
        <el-table-column label="检查组名称" align="center" prop="name"/>
        <el-table-column label="助记码" align="center" prop="helpCode"/>
        <el-table-column label="所属性别" align="center" prop="sex">
          <template slot-scope="scope">
            <p v-if="scope.row.sex == '0'">全部</p>
            <p v-if="scope.row.sex == '1'">男</p>
            <p v-if="scope.row.sex == '2'">女</p>
          </template>
        </el-table-column>
        <el-table-column label="说明" align="center" prop="attention" width="180"/>
        <el-table-column label="备注说明" align="center" prop="remark" width="180"/>
      </el-table>
      <pagination
        v-show="checktotal>0"
        :total="checktotal"
        :page.sync="queryParamsCheckItemList.pageNum"
        :limit.sync="queryParamsCheckItemList.pageSize"
        @pagination="getAddCheckGroupList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAddCheckGroup(AddCheckGroups)">确定新增</el-button>
        <el-button @click="cancelAddCheckGroup">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPost,
  delItems,
  addPost,
  updatePost,
  getPost,
  deleteSetMeal,
  getInfoBySetMeal,
  addSetMealGroups
} from '@/api/reserve/setmeal'

export default {
  name: 'Setmeal',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      //图片头的拼接
      baseUrl: process.env.VUE_APP_BASE_API,
      // 套餐中新增的检查组的数组
      AddCheckGroups: [],
      //套餐和检查组id组合数组
      SetMealCheckGroup: [],
      // 套餐和检查组数组
      items: [],
      fileList: [{ name: '', url: '' }, { name: '', url: '' }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      sdkGroupId: 0,
      checktotal: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openUpdate: false,
      openAddCheckItem: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        mealName: undefined,
        sex: undefined
      },
      queryParamsCheckItemList: {
        pageNum: 1,
        pageSize: 10,
        id: undefined
      },
      // 选择性别查询参数
      sexOptions: [{
        value: '0',
        label: '全部'
      }, {
        value: '1',
        label: '男'
      }, {
        value: '2',
        label: '女'
      }],
      // 表单参数
      form: {},
      // 检查组列表
      checkItemList: [],
      // 检查组表格数据
      groupList: [],
      // 表单校验
      rules: {
        mealName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        helpCode: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {

    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },

    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listPost(this.queryParams).then(response => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 新增检查组取消按钮
    cancelAddCheckGroup() {
      this.openAddCheckItem = false;
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 增加套餐中的检查组的多选框
    handleSelectionAddCheckGroupChange(selection) {
      this.AddCheckGroups = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 删除套餐中的检查组的多选框
    handleSelectionChangeGroups(selection) {
      this.items = selection.map(item => item)
      this.single = selection.length!=1
      this.multiple=!selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加岗位'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id ;
      this.sdkGroupId = id;
      getPost(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改套餐'
      })
    },
    reHandleUpdate(id) {
      getPost(id).then(response => {
        this.form = response.data;
        this.openUpdate = true;
        this.title = "修改套餐"
      })
    },

    /** 新增套餐中的检查组操作*/
    handleAddGroups(setmealId) {
      this.queryParamsCheckItemList.id = setmealId;
      this.getAddCheckGroupList();
      this.openAddCheckItem = true;
      this.title = "新增检查组";
    },
    getAddCheckGroupList() {
      this.loading = true;
      getInfoBySetMeal(this.queryParamsCheckItemList).then(response => {
        this.groupList = response.rows;

        this.checktotal = response.total;
        this.loading = false;
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(this.ids)
      const itemsIds = row.id || this.ids
      this.$modal.confirm('是否确认删除？').then(function() {
        return delItems(itemsIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 新增检查组到套餐中去的按钮操作*/
    submitFormAddCheckGroup(CheckGroups) {
      if (CheckGroups.length !== 0) {
        let responseMap = [];
        for (let i = 0; i < CheckGroups.length; i++) {
          responseMap.push({setmealId: this.sdkGroupId, checkgroupId: CheckGroups[i]})
        }
        addSetMealGroups(responseMap).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.openAddCheckItem = false;
          this.reHandleUpdate(this.sdkGroupId);
        });
      } else {
        this.$modal.msgError("新增失败,未选择检查组")
      }
    },
    /** 删除套餐中检查组按钮操作*/
    handelDeleteGroups(row) {
      const setmealIds = row.id || this.items
      this.$modal.confirm('确认删除?').then(function() {
        return deleteSetMeal(setmealIds)
      }).then(() => {
        getPost(setmealIds[0].setmealId).then(response => {
          this.form = response.data;
          this.$modal.msgSuccess('删除成功')
        })
      }).catch(() =>{

      })
    }
  }
}
</script>
